package com.j180.service.stoGoods.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.j180.bean.PageBean;
import com.j180.bean.PetOwner;
import com.j180.bean.StoGoods;
import com.j180.dao.stoGoods.IStoGoodsDao;
import com.j180.service.stoGoods.IStoGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("stoGoodsServiceImpl")
public class StoGoodsServiceImpl implements IStoGoodsService {

    @Autowired
    private IStoGoodsDao stoGoodsDao;

    @Override
    public PageBean<StoGoods> getAllStoGoods(Map<String, Object> map) {
        PageBean<StoGoods> pageBean = new PageBean<>();

        int currentPage = Integer.parseInt(String.valueOf(map.get("currentPage")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        int id = Integer.parseInt(String.valueOf(map.get("id")));

        PageHelper.startPage(currentPage,pageSize);
        List<StoGoods> list = stoGoodsDao.getAllStoGoods(id);
        PageInfo<StoGoods> pageInfo = new PageInfo<>(list);
        //封装自己的分页对象
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTolNum((int) pageInfo.getTotal());
        pageBean.setTolPage(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());

        return pageBean;
    }

    @Override
    public StoGoods getStoGoodsByStoreIdAndStoGoodsId(Map<String, Object> map) {
        StoGoods stoGoods = stoGoodsDao.getStoGoodsByStoreIdAndStoGoodsId(map);
        return stoGoods;
    }

    @Override
    public int addStoGoods(StoGoods stoGoods) {
        return stoGoodsDao.insertStoGoods(stoGoods);
    }

    @Override
    public int deleteStoGoodsByStoGoodsId(int id) {
        return stoGoodsDao.deleteStoGoodsByStoGoodsId(id);
    }

    @Override
    public int updateStoGoods(StoGoods stoGoods) {
        return stoGoodsDao.updateStoGoods(stoGoods);
    }

    @Override
    public List<StoGoods> getStoGoodsByStoGoodsName(String name) {
        List<StoGoods> list = stoGoodsDao.getStoGoodsByStoGoodsName("%" + name + "%");
        return list;
    }
}